<?php

/**
 * Contrôleur des flux Iphone
 */
class iphone_controller extends Component
{
    /**
     * Constructeur
     */
    public function __construct()
    {
    }

    /**
     * Données à envoyer à la vue par défaut
     */
    public function defaut()
    {
    }

    /**
     * Retourne la liste des magasins à l'iphone en format Json
     */
    public function shops()
    {
        if(isset($_POST['city_id']))
            $queryTown = $_POST['city_id'];
        else
            $queryTown = false;
        
        $this->setContentType('text/plain');
        $datas = Base::Load(CLASS_SHOP)->getAllTransform($queryTown);
        $shops = array();
        
        foreach($datas as $id => $d) {
            $data = array();
            $data['id'] = (string)$d['_id'];
            $data['nom'] = $d['name'];
            $shops['shop'][$d['type']][] = $data;
        }

        exit(json_encode(array('shops' => $shops)));
    }

    /**
     * Retourne la liste des villes
     */
    public function cities()
    {
        if (isset($_POST['city'])) {
            $queryLower = strtolower($_POST['city']);
        }

        $this->setContentType('text/plain');
        $datas = Base::Load(CLASS_LOC)->getVille();
        $villes = array();
        foreach($datas as $id => $d) {
            $data = array();
            $data['id'] = $d[0];
            $data['ville'] = $d[2];

            if (preg_match('/'.$queryLower.'/', strtolower($data['ville'])) || !isset($queryLower)) {
                $dep = Base::Load(CLASS_LOC)->getDepartement($d[1]);
                if(isset($dep[3]))
                    $data['departement'] = $dep[3];
                $data['cp'] = $d[3];
                $villes['city'][] = $data;
            }
        }

        exit(json_encode(array('cities' => $villes)));
    }

    /**
     * Connexion via Iphone
     * @return YES / NO
     */
    public function login(){
        $login = $_POST['login'];
        $pwd = $_POST['password'];

        $connection = Base::Load(CLASS_USER)->connect($login, $pwd);

        if($connection)
            exit('YES');
        else
            exit('NO');
    }

    /**
     * Retourne les infos user
     * @return null / array()
     */
    public function getUser(){
        $request = $_GET['login'];
        $info = Base::Load(CLASS_USER)->findBy('mail', $request);
        if($info){
            foreach($info as $i){
                exit((string)$i['_id'].';'.$i['mail']);
            }
        }else
            exit(null);
    }

    /*
     * Ajoute un magasin
     */
    public function shopAdd(){
        $nom = $_POST['nom'];
        $villeID = $_POST['ville_id'];
        $adress = $_POST['adresse'];
        $type = $_POST['type_magasin_id'];

        $infoVille = Base::Load(CLASS_LOC)->getVille($villeID);
        $departementID = $infoVille[1];
        $infoDepartement =  Base::Load(CLASS_LOC)->getDepartement($departementID);
        $regionID = $infoDepartement[1];

        $GPS = Tools::getGPS($infoVille[2], $adress);
        $lat = $GPS->lat;
        $lng = $GPS->lng;


        $data['name'] = utf8_encode($nom);
        $data['region'] = $regionID;
        $data['departement'] = $departementID;
        $data['ville'] = $villeID;
        $data['lat'] = $lat;
        $data['lng'] = $lng;
        $data['adress'] = utf8_encode($adress);
        $data['type'] = $type;

        if(Base::Load(CLASS_SHOP)->save($data))
            exit('YES');
        else
            exit('NO');
    }

    /**
     * Liste des chasseurs
     * @return void
     */
    public function getHunters(){
        if(KEY_IPHONE == $_POST['app_key']){
        $datas = Base::Load(CLASS_USER)->getAll();
        $chassseurs = array();
        foreach($datas as $u){
            $user['id'] = (string)$u['_id'];
            $user['pseudo'] = $u['nom'].' '.$u['prenom'];
            $user['rank'] = 1;
            $user['adresse'] = $u['adress'];
            $villeData = Base::Load(CLASS_LOC)->getVille($u['ville']);
            $user['cp'] = $villeData[3];
            $user['ville'] = $villeData[2];
            $chassseurs[] = $user;
        }
        exit(json_encode(array('chasseurs' => $chassseurs)));
        }else
            exit();
    }

    /*
     * id, pseudo, rank, adresse, cp, ville
     */

}


/* -- Fin du fichier -- */